Step 7: API Server

In the previous section, we used Express to serve a simple website. Here, we will make a simple API server.

Create a new folder, hello-api, and initialize a Node package in it:

yarn init -y

Next, install Express.

yarn add express

Let’s also add Nodemon as a dev dependency:

yarn add -D nodemon

Next, add the type and scripts to the package.json:

{
  "name": "hello-api",
  "version": "1.0.0",
  "main": "server.js",
  "license": "MIT",
  "type": "module",
  "scripts": {
    "start": "node server.js",
    "dev": "nodemon server.js"
  },
  "dependencies": {
    "express": "^4.18.2"
  },
  "devDependencies": {
    "nodemon": "^2.0.20"
  }
}

Notice now the yarn dev command will use the nodemon package to run our server. Nodemon improves the developer experience by automatically restarting the node application when file changes in the directory are detected.

Add a server.js file:

import express from "express";

const port = 3000;
const app = express();

app.get("/", (req, res) => {
  res.send("Hello Express");
});

app.listen(port, () => {
  console.log(`Server running at http://localhost:${port}/`);
});

Run the server with yarn dev.

Next, add a data.js file:

data.js
export const schools = [
  {
    Name: "Whiting School of Engineering",
  },
];

export const terms = [
  {
    Name: "Fall 2019",
  },
  {
    Name: "Spring 2020",
  },
  {
    Name: "Fall 2020",
  },
  {
    Name: "Spring 2021",
  },
  {
    Name: "Fall 2021",
  },
  {
    Name: "Spring 2022",
  },
  {
    Name: "Fall 2022",
  },
];

export const courses = [
  {
    OfferingName: "EN.500.112",
    Title: "Gateway Computing: JAVA",
    Term: "Fall 2019",
  },
  {
    OfferingName: "EN.601.226",
    Title: "Data Structures",
    Term: "Fall 2019",
  },
  {
    OfferingName: "EN.601.226",
    Title: "Data Structures",
    Term: "Spring 2020",
  },
  {
    OfferingName: "EN.601.421",
    Title: "Object Oriented Software Engineering",
    Term: "Spring 2020",
  },
  {
    OfferingName: "EN.601.226",
    Title: "Data Structures",
    Term: "Fall 2020",
  },
  {
    OfferingName: "EN.601.280",
    Title: "Full-Stack JavaScript",
    Term: "Fall 2020",
  },
  {
    OfferingName: "EN.601.226",
    Title: "Data Structures",
    Term: "Spring 2021",
  },
  {
    OfferingName: "EN.601.421",
    Title: "Object Oriented Software Engineering",
    Term: "Spring 2021",
  },
  {
    OfferingName: "EN.601.226",
    Title: "Data Structures",
    Term: "Fall 2021",
  },
  {
    OfferingName: "EN.601.280",
    Title: "Full-Stack JavaScript",
    Term: "Fall 2021",
  },
  {
    OfferingName: "EN.601.226",
    Title: "Data Structures",
    Term: "Spring 2022",
  },
  {
    OfferingName: "EN.601.421",
    Title: "Object Oriented Software Engineering",
    Term: "Spring 2022",
  },
  {
    OfferingName: "EN.601.226",
    Title: "Data Structures",
    Term: "Fall 2022",
  },
  {
    OfferingName: "EN.601.280",
    Title: "Full-Stack JavaScript",
    Term: "Fall 2022",
  },
];

Now update the server.js as follows:

import express from "express";
import { schools, terms, courses } from "./data.js";

const port = 3000;
const app = express();

app.get("/", (req, res) => {
  res.send("Welcome to Ali's resume at Hopkins");
});

app.get("/schools", (req, res) => {
  res.json({
    data: schools,
  });
});

app.get("/terms", (req, res) => {
  res.json({ 
		data: terms 
	});
});

app.get("/courses", (req, res) => {
  res.json({
    data: courses
  });
});

app.listen(port, () => {
  console.log(`Server running at http://localhost:${port}/`);
});

Open your browser and try the following endpoints:

Try these endpoints in the Postman, too (as HTTP Get requests). The example above showcases the essence of how API servers are built using Node and Express. We will explore this further in future lessons.